package com.itgaoshu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.Departments;
import com.itgaoshu.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 科室中心
 */
//@Controller
@RestController
@RequestMapping("/department")
public class DepartmentsController {
    //依赖注入
    @Autowired
    private DepartmentsService departmentsService;
    //查询所有科室
    @RequestMapping("selectAllDepartments")
    //@ResponseBody//返回json格式
    public Object selectAllDepartments(@RequestParam(required = true,defaultValue = "1") int pageNum){
        //通过PageHelper调用startPage方法设置查询的页数和每页查询的记录条数
        PageHelper.startPage(pageNum,5);//每页5条数据
        List<Departments> allDepartments =departmentsService.selectAllDepartments();
        //将查询的结果以及分页所需的数据封装到pageInfo对象中
        PageInfo pageInfo=new PageInfo(allDepartments);
        System.out.print(pageInfo);
        Map<String,Object> allDepartment=new HashMap<>();
        //这是LayUI要求返回的json数据形式
        allDepartment.put("code",0);
        allDepartment.put("msg","");
        //将全部数据的条数作为count传给前台（一共有多少条数据）
        allDepartment.put("count",pageInfo.getTotal());
        //将分页后的数据返回（每一页显示的数据）
        allDepartment.put("data",pageInfo.getList());
        return allDepartment;
    };
    //添加科室
    @RequestMapping("addDepartments")
    public Object addDepartments(Departments departments) {
       int result=departmentsService.addDepartments(departments);
       if(result==0){
           int add=departmentsService.addDepartments(departments);
           if(add==1){//添加成功
              // return "view/center/departments";
               return "添加成功";
           }else {
               //return "addDepartments.do";
               return "添加失败";
           }
       }else {
           return  departments.getDepartment()+"已存在";
       }

    };
    //模糊查询
    @RequestMapping("selectDepartments")
    @ResponseBody//返回json格式
    public Object selectDepartments(Departments departments,@RequestParam(required = true,defaultValue = "1") int pageNum){
        //通过PageHelper调用startPage方法设置查询的页数和每页查询的记录条数
        PageHelper.startPage(pageNum,5);//每页5条数据
        List<Departments> allDepartments =departmentsService.selectDepartments(departments);
        //将查询的结果以及分页所需的数据封装到pageInfo对象中
        PageInfo pageInfo=new PageInfo(allDepartments);
        System.out.print(pageInfo);
        Map<String,Object> allDepartment=new HashMap<>();
        //这是LayUI要求返回的json数据形式
        allDepartment.put("code",0);
        allDepartment.put("msg","");
        //将全部数据的条数作为count传给前台（一共有多少条数据）
        allDepartment.put("count",pageInfo.getTotal());
        //将分页后的数据返回（每一页显示的数据）
        allDepartment.put("data",pageInfo.getList());
        return allDepartment;
    };
}
